layui.config({
    base: "/layui/"
}).extend({
    index: 'lib/index'
}).use(['index', 'form', 'jquery', 'table', 'upload'], function () {
    var form = layui.form,
        table = layui.table,
        upload = layui.upload,
        admin = layui.admin;

    /**
     * 防重复设置
     * @type {boolean}
     */
    var edit_flag = true;

    table.render({
        elem: '#department_result'
        , url: '/deviceOrder/queryData'
        , method: 'post'
        , title: ''
        , height: 500
        , limit: 50							//显示数目
        , limits: [50, 100]
        , cols: [[
            {field: 'id', title: 'id', width: 200, "align": "center", hide: true}
            , {field: 'ownerName', title: '买方名称', width: 200, "align": "center"}
            , {field: 'cameraSn', width: 220, title: '摄像头sn序列号', "align": "center"}
            , {field: 'terminalSn', title: '设备sn序列号', width: 220, "align": "center"}
            , {field: 'orgName', title: '归属公司名称', width: 200, "align": "center"}
            , {field: 'deviceType', title: '设备类型', width: 150, "align": "center", templet: function (d) {
                    if (d.deviceType == "WECHAT") {
                        return "微信";
                    } else if (d.deviceType == "ALIPAY") {
                        return "支付宝";
                    } else{
                        return "";
                    }
                }}
            , {field: 'remarks', title: '备注', width: 140, "align": "center"}
            , {field: 'createdTime', title: '创建时间', width: 180, "align": "center"}
            , {field: 'OPT', width: 150, title: '操作', "align": "center", toolbar: "#table-tool"}
        ]]
        , request: {
            pageName: 'pageNo' //页码的参数名称，默认：page
            , limitName: 'pageSize' //每页数据量的参数名，默认：LIMIT
        },
        where: {

        }
        , id: 'department_result'
        , page: true
    });


    //查询按钮事件
    $(".order_btn").click(function () {
        table.reload('department_result', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            , where: {
                terminalSn: $("#terminalSn").val()
            }
        });
    });

    /**
     * 选择设备转出所需要接收的代理商
     */
    table.on('tool(department_result)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的DOM对象
        if (layEvent === 'assignment') { //转让确认
            layer.open({
                type: 1,
                area: ['750px', '650px'],
                title: "设备转出",
                fixed: false, //不固定
                maxmin: true,
                maxmin: false,
                shadeClose: false,
                content: $('#user_win')
            });
            //表单值
            form.val("device_order_form", {
                'deviceOrderId': data.id,
                'cameraSn': data.cameraSn,
                'terminalSna': data.terminalSn,
                'deviceType': data.deviceType
            });
            table.render({
                elem: '#agency_result'
                , url: '/oem_user/agencyData'
                , method: 'post'
                , title: '代理商列表'
                , height: 500
                , limit: 50							//显示数目
                , limits: [50, 100]
                , cols: [[
                    {field: 'id', title: '用户编号', width: 100, "align": "center"}
                    , {field: 'userName', title: '用户名称', width: 180, "align": "center"}
                    , {field: 'mobile', title: '手机号码', width: 150, "align": "center"}
                    , {
                        field: 'type', width: 130, title: '用户类型', "align": "center", templet: function (d) {
                            if (d.type == "AGENCY") {
                                return "代理商";
                            }
                        }
                    }
                    , {fixed: 'right', title: '操作', toolbar: '#hidden1-table-tool', width: 80}
                ]]
                , request: {
                    pageName: 'pageNo' //页码的参数名称，默认：page
                    , limitName: 'pageSize' //每页数据量的参数名，默认：LIMIT
                },
                where: {

                }
                , id: 'agency_result'
                , page: true
            });
        }
    });

    //代理商查询按钮事件
    $(".agency_btn").click(function () {

        table.reload('agency_result', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            , where: {
                userName: $(".agencyTable input[name='agencyName']").val()
            }
        });
    });

    /**
     * 选择按钮
     */
    table.on('tool(agency_result)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的DOM对象
        var userId = data.id;//得到接收人id
        var deviceOrderId = $("#deviceOrderId").val();//得到设备订单id
        var cameraSn = $("#cameraSn").val();//得到摄像头sn
        var terminalSna = $("#terminalSna").val();//得到设备sn
        var deviceType = $("#deviceType").val();//得到设备sn
        if (layEvent === 'yes') { //转让确认
            layer.confirm("请确认", function (index) {
                $.ajax({
                    url: "/deviceOrder/assignmentDevice",
                    data: {userId: userId, deviceOrderId: deviceOrderId, cameraSn: cameraSn, terminalSn: terminalSna,type: deviceType},
                    type: "post",
                    success: function (result) {
                        if (result.message == "操作成功") {
                            layer.msg("转出成功", {
                                offset: '32%'
                                , icon: 1
                                , time: 0
                                , btn: '知道了'
                                , btnAlign: "c"
                                , shade: 0.4
                                , yes: function () {
                                    table.reload('department_result', {});
                                    layer.closeAll();
                                }
                            })
                        } else {
                            layer.msg(result.message, {
                                offset: '32%'
                                , icon: 2
                                , time: 3000
                            }, function () {
                                layer.closeAll();
                            })
                        }
                    }
                });
            });
        }
    });
});